package com.spdg.ring.task;

import android.content.Context;
import cn.wolf.base.BaseTask;
import cn.wolf.http.BaseResp;
import cn.wolf.http.HttpCallBack;
import cn.wolf.http.HttpRequest;
import cn.wolf.tools.AppHelper;
import cn.wolf.tools.JsonUtil;
import cn.wolf.tools.Log;

import com.spdg.ring.common.Config;
import com.spdg.ring.entity.VersionEntity;
import com.spdg.ring.resp.VersionResp;

/**
 * <p>Title:  升级检测</p>
 * <p>Description:  </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-22             </p>
 * <p>Update Time: 2012-11-15                    </p>
 * <p>Updater:   Eric.wsd                      </p>
 * <p>Update Comments:                 </p>
 */
public class CheckVersionTask extends BaseTask<Void, Void, VersionResp> {

	public CheckVersionTask(HttpCallBack<VersionResp> iCall, Context context) {
		super(iCall, context);
	}

	@Override
	protected BaseResp doInBackground(Void... params) {
		HttpRequest request = new HttpRequest(VersionResp.class);
		VersionResp resp = new VersionResp();
		try {
			int versionCode = AppHelper.getVersionCode(mContext);
			String packageName = AppHelper.getPackageName(mContext);
			
			String url = Config.URL.URL_CHECK_UPDATE;
			request.addParameter("version", versionCode+"");
			request.addParameter("appid", packageName);
			resp = (VersionResp) request.execute(url);
			
			if(resp.isSuccess()) {
				VersionEntity entity = JsonUtil.toObject(resp.getHttpResult(), VersionEntity.class);
				resp.setEntity(entity);
			}
		} catch (Exception e) {
			Log.e("检查更新异常：" + e);
		} finally {
			request.release();
		}

		return resp;
	}

}
